package xin.marcher.wind.process.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.mybatis.dao.BaseDAO;
import xin.marcher.wind.process.domain.entity.ProcessConfigDO;
import xin.marcher.wind.process.mapper.ProcessConfigMapper;

import java.util.List;

/**
 * 流程配置表 DAO
 * </p>
 *
 * 
 */
@Repository
public class ProcessConfigDAO extends BaseDAO<ProcessConfigMapper, ProcessConfigDO> {

    /**
     * 根据name查询流程配置
     *
     * @param name
     * @return
     */
    public ProcessConfigDO getOne(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        LambdaQueryWrapper<ProcessConfigDO> query = new LambdaQueryWrapper<>();
        query.eq(ProcessConfigDO::getName, name);
        List<ProcessConfigDO> list = list(query);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public List<ProcessConfigDO> listBy(List<Integer> enables) {
        LambdaQueryWrapper<ProcessConfigDO> query = new LambdaQueryWrapper<>();
        query.in(ProcessConfigDO::getEnable, enables);
        return list(query);
    }

    public void deleteByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return;
        }
        LambdaQueryWrapper<ProcessConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessConfigDO::getName, name);

        remove(queryWrapper);
    }

    public void enable(String name, Integer enable) {
        LambdaUpdateWrapper<ProcessConfigDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ProcessConfigDO::getEnable,enable)
                .eq(ProcessConfigDO::getName,name);
        update(updateWrapper);
    }
}
